package com.travel.find.findactivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.travel.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/18.
 * 加载recyclerview的item跳转的数据
 */
public class FindRVActivity extends AppCompatActivity {
    @Bind(R.id.ic_rvactivity_back)
    ImageView icRvactivityBack;
    @Bind(R.id.iv_rvactivity_collect)
    ImageView ivRvactivityCollect;
    @Bind(R.id.iv_rvactivity_share)
    ImageView ivRvactivityShare;
    @Bind(R.id.wv_rvactivitiy)
    WebView wvRvactivitiy;
    @Bind(R.id.tc_rvactivity_title)
    TextView tcRvactivityTitle;
    //webview网址
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_rv_activity);
        ButterKnife.bind(this);
        //初始化数据
        initview();
        //点击返回按钮返回
        initback();
        //添加webview数据
        initwebview();
    }

    //点击返回键判断是否是主webview界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (wvRvactivitiy.canGoBack()) {
                wvRvactivitiy.goBack();//goBack() 表示返回webview中的上一页面
            } else {
                finish();
            }
        }
        return true;
    }

    //接收传过来的数据
    private void initview() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        String title = intent.getStringExtra("title");
        tcRvactivityTitle.setText(title);
    }

    //点击按钮返回
    private void initback() {
        icRvactivityBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (wvRvactivitiy.canGoBack()) {
                    wvRvactivitiy.goBack();//goBack() 表示返回webview中的上一页面
                } else {
                    finish();
                }
            }
        });
    }

    private void initwebview() {
        //设置webiew的背景颜色
        wvRvactivitiy.setBackgroundColor(Color.WHITE);
        //设置字符集编码
        wvRvactivitiy.getSettings().setDefaultTextEncodingName("utf-8");
        //设置webview的缩放功能  设置webview触摸放大缩小
        wvRvactivitiy.getSettings().setSupportZoom(true);
        wvRvactivitiy.getSettings().setBuiltInZoomControls(true);
        //设置webview中滚动条的样式  SCROLLBARS_OUTSIDE_INSET  SCROLLBARS_OUTSIDE_OVERLAY
        //SCROLLBARS_INSIDE_INSET SCROLLBARS_INSIDE_OVERLAY
        wvRvactivitiy.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        wvRvactivitiy.setHorizontalScrollBarEnabled(false);//禁止水平滚动条
        wvRvactivitiy.setWebChromeClient(new WebChromeClient());
        //设置当webview双击时变大 再双击就变小  当手动放大时  双击可以恢复到原始大小
        wvRvactivitiy.getSettings().setUseWideViewPort(true);
        //设置webview的初始化尺寸  参数百分比
        wvRvactivitiy.setInitialScale(100);
        //点击网页中的超链接时不弹出浏览器的窗口 在webview控件中加载url
        wvRvactivitiy.setWebViewClient(new WebViewClient());
        //允许js执行 如果访问的html页面中存在JavaScript脚本语言  webview中需要设置支持
        wvRvactivitiy.getSettings().setJavaScriptEnabled(true);
        wvRvactivitiy.loadUrl(url);
    }
}
